﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class EditStylesheet : AdminPage, IEditStylesheetView
    {
        private EditStylesheetPresenter _presenter;

        #region IEditStylesheetView Members

        public int StylesheetID
        {
            get
            {
                if (ViewState["StylesheetID"] == null)
                    ViewState["StylesheetID"] = QueryString.GetInt32ID("id");
                return (int) ViewState["StylesheetID"];
            }
        }


        public void SetStylesheetID(int id)
        {
            ViewState["StylesheetID"] = id;
        }


        public string Name
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }


        public void SetContexts(IDictionary<StylesheetContext, string> contexts)
        {
            ddlContexts.Items.Clear();
            foreach (var kvp in contexts)
                ddlContexts.Items.Add(new ListItem(kvp.Value, ((int) (kvp.Key)).ToString()));
        }


        public StylesheetContext SelectedContext
        {
            get { return (StylesheetContext) int.Parse(ddlContexts.SelectedValue); }
            set { ddlContexts.SelectedValue = value.ToString(); }
        }


        public string SourceCode
        {
            get { return hidCode.Value; }
            set { hidCode.Value = value; }
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/EditStylesheet.xml"; }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new EditStylesheetPresenter(this);
            int selectedContextIndex = ddlContexts.SelectedIndex;
            _presenter.SetContexts();
            ddlContexts.SelectedIndex = selectedContextIndex;
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
                hlCancel.NavigateUrl = !string.IsNullOrEmpty(Request.QueryString["referrer"])
                                           ? "Admin/" + Request.QueryString["referrer"]
                                           : "Admin/Stylesheets.aspx";
            }
            if (StylesheetID > 0)
                HeadingText = GetText(this, "EditStylesheetHeading") + " &mdash; " + Name;
            else
                HeadingText = GetText(this, "NewStylesheetHeading");
        }


        protected void lbSave_Click(object sender, EventArgs e)
        {
            _presenter.Save();
        }


        protected void lbSaveAndReturn_Click(object sender, EventArgs e)
        {
            _presenter.Save();
            if (!_presenter.IsValid)
                return;
            Response.Redirect(!string.IsNullOrEmpty(Request.QueryString["referrer"])
                                  ? Request.QueryString["referrer"]
                                  : "Stylesheets.aspx");
        }
    }
}